// Copyright (C) 2016 All rights reserved.
// Author: liufeng11@letv.com (Liu Feng)
// Description:
#include "MySimilarity.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>

double MySimilarity::GetSimilarity(const std::vector<double>& feature1, const std::vector<double>& feature2) {
  return GetCosine(feature1, feature2);
}


double MySimilarity::GetCosine(const std::vector<double>& feature1, const std::vector<double>& feature2) {
  if (feature1.size() != feature2.size()) {
    printf("%s:%d : %s\n", __FILE__, __LINE__, __FUNCTION__);
    printf("feature1 dim: %d, feature2 dim: %d\n", feature1.size(), feature2.size());
    exit(-1);
  }

  int length = feature1.size();
  
  double product = 0;
  double sum1 = 0;
  double sum2 = 0;

  for (int i = 0; i < length; ++i) {
    product += feature1[i]*feature2[i];
    sum1 += feature1[i]*feature1[i];
    sum2 += feature2[i]*feature2[i];
  }

  double r = product/sqrt(sum1*sum2);
  return 1/(1e-5+r);
}

